package com.boot.producer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.boot.producer.entity.Violation;
import com.boot.producer.mapper.ViolationMapper;
import com.boot.producer.service.ViolationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.boot.utils.DateUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangZhilei
 * @since 2019-02-25
 */
@Service
public class ViolationServiceImpl extends ServiceImpl<ViolationMapper, Violation> implements ViolationService {
    @Override
    public Map<Integer, Integer> listTimesByDate(Date date) {
        List<Violation> violations = this.baseMapper.selectList(new EntityWrapper<Violation>().eq("date", date));
        return getTimes(violations);
    }

    /**
     * @return Map<Integer ,   I nteger> 类型：次数
     * @description 获取一周信息
     * @author 张志磊
     * @date 2019/2/26 11:21
     */
    @Override
    public Map<Integer, List<Integer>> getAnalysisData(Date begin, Date end) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Integer> list4 = new ArrayList<>();
        List<Integer> list5 = new ArrayList<>();
        List<Long> days = new ArrayList<>();
        for (int i = DateUtil.differentDays(begin, end); i >= 0; i--) {
            list2.add(0);
            list3.add(0);
            list4.add(0);
            list5.add(0);
            if (days.contains(DateUtil.getStartOfADay(DateUtil.changeDay(-i)).getTime())){
                System.out.println("aaa");
            }
            days.add(DateUtil.changeDay(end, -i).getTime());
            System.out.println(DateUtil.getStartOfADay(DateUtil.changeDay(-i)));
            System.out.println(DateUtil.changeDay(-i));
        }
        System.out.println(DateUtil.getStartOfADay(new Date()));
        List<Violation> violations = this.baseMapper.selectList(new EntityWrapper<Violation>().between("date", begin, end).orderBy("date"));
        for (Violation violation:violations){
            Integer index = days.indexOf(violation.getDate().getTime());
            if (violation.getType() == 2){
                list2.set(index,list2.get(index)+1);
            } else if (violation.getType() == 3){
                list3.set(index,list3.get(index)+1);
            } else if (violation.getType() == 4){
                list4.set(index,list4.get(index)+1);
            } else if (violation.getType() == 5){
                list5.set(index,list5.get(index)+1);
            }
        }
        map.put(2,list2);
        map.put(3,list3);
        map.put(4,list4);
        map.put(5,list5);
        return map;
    }

    /**
     * @return
     * @description 获取近一周数据
     * @author 张志磊
     * @date 2019/2/26 15:08
     */
    @Override
    public Map<Integer, Integer> getWeeklyData() {
        List<Violation> violations = this.baseMapper.selectList(new EntityWrapper<Violation>().between("date", DateUtil.backDate(), DateUtil.getEndOfADay(new Date())).orderBy("date"));
        return getTimes(violations);
    }

    /**
     * @param begin
     * @param end
     * @return Map<Integer ,   I nteger>
     * @description 根据范围获取数据
     * @author 张志磊
     * @date 2019/2/26 15:08
     */
    @
            Override
    public Map<Integer, Integer> getByRange(Date begin, Date end) {
        List<Violation> violations = this.baseMapper.selectList(new EntityWrapper<Violation>().between("date", begin, end));
        return getTimes(violations);
    }

    private Map<Integer, Integer> getTimes(List<Violation> violations) {
        Map<Integer, Integer> map = new HashMap();
        for (int i = 1; i <= 6; i++) {
            map.put(i, 0);
        }
        for (Violation violation :
                violations) {
            map.put(violation.getType(), map.get(violation.getType()) + 1);
        }
        return map;
    }

//    private Map<Integer,Integer> getTimes(){
//
//    }
}
